##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
## Engaged or Obedient?  Racially Differentiated Models of Democratic Education
## Replication File- Influence of Class

#Inputs:non_white_free_lunch.RData, non_white_no_lunch.RData, white_free_lunch.RData, white_no_lunch.RData, class_white.RData, class_non_white.RData, citizen_lunch.csv
#Outputs: Figure E.1,Figure E.2, Figure E.3, Figure E.4, Figure E.5
##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##

rm(list=ls())

library(readr)
library(quanteda)
library(quanteda.corpora)
library(ggcorrplot)
library(ggplot2)
library(dplyr)
library(lubridate)
library(stats)
library(patchwork)
library(tm)
library(coefplot)


## Set  working directory
setwd("~/Desktop/TAD Handbooks Project/engaged_obedient_replication")

#Figure E.1
non_white_free_lunch <- load("data/non_white_free_lunch.RData")
non_white_no_lunch <- load("data/non_white_no_lunch.RData")

topfeatures(free_lunch_dfm_trim, 30)
topfeatures(no_lunch_dfm_trim, 30)

tstat_freq <- textstat_frequency(free_lunch_dfm_trim, n = 30)
head(tstat_freq, n=30)

p1<- ggplot(data = tstat_freq, aes(x = nrow(tstat_freq):1, y = frequency)) +
  geom_point(stat = 'identity') +
  facet_wrap(~ group, scales = "free") +
  coord_flip() +
  scale_x_continuous(breaks = nrow(tstat_freq):1,
                     labels = tstat_freq$feature) +
  labs(title = "When >70% Students Qualify for FRL", x = NULL, y = "Frequency")

p1

tstat_freq <- textstat_frequency(no_lunch_dfm_trim, n = 30)
head(tstat_freq, n=30)

p2<- ggplot(data = tstat_freq, aes(x = nrow(tstat_freq):1, y = frequency)) +
  geom_point(stat = 'identity') +
  facet_wrap(~ group, scales = "free") +
  coord_flip() +
  scale_x_continuous(breaks = nrow(tstat_freq):1,
                     labels = tstat_freq$feature) +
  labs(title = " When < 30% Students Qualify for FRL", x = NULL, y = "Frequency")

p2

#Join figures for visual
fig1 <- (p1 + p2) + plot_annotation(title="Top Features in Majority Non-White Schools (>70%)")

#Save visual to "figures" folder in working director
ggsave("figures/engaged_obedient_E_figure1.pdf", width = 11, height = 7)
ggsave("figures/engaged_obedient_E_figure1.jpeg", width = 11, height = 7)


##Figure E.2
white_free_lunch <- load("data/white_free_lunch.RData")
white_no_lunch <- load("data/white_no_lunch.RData")

topfeatures(free_lunch_dfm_trim, 30)
topfeatures(no_lunch_dfm_trim, 30)


tstat_freq <- textstat_frequency(free_lunch_dfm_trim, n = 30)
head(tstat_freq, n=30)

p1<- ggplot(data = tstat_freq, aes(x = nrow(tstat_freq):1, y = frequency)) +
  geom_point(stat = 'identity') +
  facet_wrap(~ group, scales = "free") +
  coord_flip() +
  scale_x_continuous(breaks = nrow(tstat_freq):1,
                     labels = tstat_freq$feature) +
  labs(title = "When > 50% Students Qualify for FRL", x = NULL, y = "Frequency")

p1

tstat_freq <- textstat_frequency(no_lunch_dfm_trim, n = 30)
head(tstat_freq, n=30)

p2<- ggplot(data = tstat_freq, aes(x = nrow(tstat_freq):1, y = frequency)) +
  geom_point(stat = 'identity') +
  facet_wrap(~ group, scales = "free") +
  coord_flip() +
  scale_x_continuous(breaks = nrow(tstat_freq):1,
                     labels = tstat_freq$feature) +
  labs(title = "When < 50% Students Qualify for FRL", x = NULL, y = "Frequency")

p2

#Join figures for visual
fig2 <- (p1 + p2) + plot_annotation(title="Top Features in Majority White Schools (>70%)")

#Save visual to "figures" folder in working director
ggsave("figures/engaged_obedient_E_figure2.pdf", width = 11, height = 7)
ggsave("figures/engaged_obedient_E_figure2.jpeg", width = 11, height = 7)


#Figure E.3
class_white <-load("data/class_white.RData")
class_non_white <-load("data/class_non_white.RData")

topfeatures(white_dfm_trim, 30)
topfeatures(non_white_dfm_trim, 30)

tstat_freq <- textstat_frequency(white_dfm_trim, n = 30)
head(tstat_freq, n=30)

p1<- ggplot(data = tstat_freq, aes(x = nrow(tstat_freq):1, y = frequency)) +
  geom_point(stat = 'identity') +
  facet_wrap(~ group, scales = "free") +
  coord_flip() +
  scale_x_continuous(breaks = nrow(tstat_freq):1,
                     labels = tstat_freq$feature) +
  labs(title = "Majority White Schools (>70%)", x = NULL, y = "Frequency")

p1

tstat_freq <- textstat_frequency(non_white_dfm_trim, n = 30)
head(tstat_freq, n=30)

p2<- ggplot(data = tstat_freq, aes(x = nrow(tstat_freq):1, y = frequency)) +
  geom_point(stat = 'identity') +
  facet_wrap(~ group, scales = "free") +
  coord_flip() +
  scale_x_continuous(breaks = nrow(tstat_freq):1,
                     labels = tstat_freq$feature) +
  labs(title = "Majority Non-White Schools (>70%)", x = NULL, y = "Frequency")

p2

#Join figures for visual
fig3 <- (p1 + p2) + plot_annotation(title="Top Features- < 30% Students Qualify for FRL")

#Save visual to "figures" folder in working director
ggsave("figures/engaged_obedient_E_figure3.pdf", width = 11, height = 7)
ggsave("figures/engaged_obedient_E_figure3.jpeg", width = 11, height = 7)




### Appendix E Figure 4
#load citizenship data with free and reduced lunch (FRL variable)
rm(list=ls())
citizen_lunch <-read.csv("data/citizen_lunch.csv")
merged=citizen_lunch
merged <- subset(merged, EXCLUDE==0)


#subset data by race
non_white <- subset(merged, white<=.29999)
white <- subset(merged, white>=.70001)

# Non-white schools
# Create frl dummy
maj_frl <- subset(non_white, free_lunch>=.50001)
maj_frl$frl_dummy <-"1"

maj_non_frl <- subset(non_white, free_lunch<=.4999)
maj_non_frl$frl_dummy <-"0"

handbooks_dummy <- rbind(maj_frl, maj_non_frl)

#Difference in Means Test for Variables of Interest-Classical, Productive, Responsibility
handbooks_dummy$Free_Lunch<-handbooks_dummy$frl_dummy
Responsibility<-lm(responsibility~Free_Lunch, data=handbooks_dummy)
summary(Responsibility)
Productive<-lm(productive~Free_Lunch, data=handbooks_dummy)
summary(Productive)
Classical<-lm(classical~Free_Lunch, data=handbooks_dummy)
summary(Classical)

p1<-multiplot(Responsibility, Productive, Classical, 
              intercept=FALSE, ylab="Free and Reduced Lunch (>50%)", title="Majority Non-White Schools (>70%)",
              zeroColor = "grey", fillColor="white", plot.linetypes=TRUE, legend.position=c("left"))
p1

# White Schools
# Create frl dummy
maj_frl <- subset(white, free_lunch>=.50001)
maj_frl$frl_dummy <-"1"

maj_non_frl <- subset(white, free_lunch<=.4999)
maj_non_frl$frl_dummy <-"0"

handbooks_dummy <- rbind(maj_frl, maj_non_frl)

#Difference in Means Test for Variables of Interest- Classical, Productive, Responsibility
handbooks_dummy$Free_Lunch<-handbooks_dummy$frl_dummy
Responsibility<-lm(responsibility~Free_Lunch, data=handbooks_dummy)
summary(Responsibility)
Productive<-lm(productive~Free_Lunch, data=handbooks_dummy)
summary(Productive)
Classical<-lm(classical~Free_Lunch, data=handbooks_dummy)
summary(Classical)

p2<- multiplot(Responsibility, Productive, Classical, 
               intercept=FALSE, ylab="Free and Reduced Lunch (>50%)", title="Majority White Schools (>70%)",
               zeroColor = "grey", fillColor="white", plot.linetypes=TRUE, legend.position=c("left"))


p2

fig4 <- (p1 + p2) + plot_annotation(title="")

ggsave("figures/engaged_obedient_E_figure_4.pdf", width = 11, height = 7)
ggsave("figures/engaged_obedient_E_figure_4.jpeg", width = 11, height = 7)


# Appendix E Figure 5
#subset data by class
handbooks_no_frl <- subset(merged, free_lunch<=.3)
handbooks_frl <- subset(merged, free_lunch>=.7)

# Create race dummy 
maj_white <- subset(handbooks_no_frl, white>=.70001)
maj_white$white_dummy <-"1"

maj_non_white <- subset(handbooks_no_frl, white<=.29999)
maj_non_white$white_dummy <-"0"

handbooks_dummy <- rbind(maj_white, maj_non_white)

handbooks_dummy$White<-handbooks_dummy$white_dummy
Responsibility<-lm(responsibility~White, data=handbooks_dummy)
summary(Responsibility)
Productive<-lm(productive~White, data=handbooks_dummy)
summary(Productive)
Classical<-lm(classical~White, data=handbooks_dummy)
summary(Classical)


p1<-multiplot(Responsibility, Productive, Classical, 
              intercept=FALSE, ylab="Majority White Schools (>70%)", title="When < 30% Students Qualify for FRL",
              zeroColor = "grey", fillColor="white", plot.linetypes=TRUE, legend.position=c("left"))

p1



#Repeat for 70% or more students qualify
# Create race dummy 
maj_white <- subset(handbooks_frl, white>=.50001)
maj_white$white_dummy <-"1"

maj_non_white <- subset(handbooks_frl, white<=.49999)
maj_non_white$white_dummy <-"0"

handbooks_dummy <- rbind(maj_white, maj_non_white)

handbooks_dummy$White<-handbooks_dummy$white_dummy
Responsibility<-lm(responsibility~White, data=handbooks_dummy)
summary(Responsibility)
Productive<-lm(productive~White, data=handbooks_dummy)
summary(Productive)
Classical<-lm(classical~White, data=handbooks_dummy)
summary(Classical)

#Appendix E, Figure 5
p2<- multiplot(Responsibility, Productive, Classical, 
               intercept=FALSE, ylab="Majority White Schools (>50%)", title="When > 70% Students Qualify for FRL",
               zeroColor = "grey", fillColor="white", plot.linetypes=TRUE, legend.position=c("left"))

p2


fig5 <- (p1 + p2) + plot_annotation(title="")

ggsave("figures/engaged_obedient_E_figure5.pdf", width = 11, height = 7)
ggsave("figures/engaged_obedient_E_figure5.jpeg", width = 11, height = 7)




#Data for proportion discussed in Appendix E
classical <- handbooks_dummy[c("classical", "white_dummy")]
classical <- na.omit(classical)
prop_classical<-classical%>%
  group_by(white_dummy)%>%
  summarise(prop_classical=sum(classical/n()))


